import 'dart:io';

import 'package:bloctest/pages/home_page/app.dart';
import 'package:bloctest/utils/Constants.dart';
import 'package:bloctest/utils/sp_utils.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

import 'utils/http/dio_instance.dart';

void main() async {
  //ssl
  HttpOverrides.global = MyHttpOverrides();
  //初始化dio
  initDio();
  //初始化权限
  initPer();
  //初始化SharedPreferences
  await SpUtils.init();
  SpUtils.remove(Constants().COOKIE_LIST);
  runApp(const MyApp());
}

class MyHttpOverrides extends HttpOverrides {
  @override
  HttpClient createHttpClient(SecurityContext? context) {
    return super.createHttpClient(context)
      ..badCertificateCallback =
          (X509Certificate cert, String host, int port) => true;
  }
}

void initDio() {
  DioInstance instance = DioInstance.instance();
  instance.initDio("https://www.wanandroid.com/");
}

void initPer() {
  // 获取可用的摄像头列表
  WidgetsFlutterBinding.ensureInitialized();
  requestCameraPermission();
}

//申请相机权限
Future<bool> requestCameraPermission() async {
  //获取当前的权限
  var status = await Permission.camera.status;
  if (status == PermissionStatus.granted) {
    print('已授权');
    //已经授权
    return true;
  } else {
    //未授权则发起一次申请
    status = await Permission.camera.request();
    print('未授权');
    if (status == PermissionStatus.granted) {
      return true;
    } else {
      return false;
    }
  }
}
